package com.example.shirotest.console;

import com.example.shirotest.entry.Sites;
import com.example.shirotest.utils.ToolUtil;

import java.util.concurrent.ArrayBlockingQueue;

public class Consumer extends Thread{
    private GetRemoteData shareData;

    public Consumer(String name,GetRemoteData shareData){
        super(name);
        this.shareData = shareData;
    }

    @Override
    public void run() {
        Integer waittime = 500;
        while (true){
            if(shareData.getIsEnd()){
                System.out.println(ToolUtil.getCurDateTimeStr() + Thread.currentThread().getName()+ " say: the sites list is over , I will finished ");
                break;
            }

            ArrayBlockingQueue<Sites> load = shareData.getData();
            Sites poll = load.poll();

            if(null==poll){
                System.out.println(ToolUtil.getCurDateTimeStr() + Thread.currentThread().getName()+ " say: the queue is empty , wait 10s ");
                waittime = 10000;
            }else {
                System.out.println(ToolUtil.getCurDateTimeStr() + Thread.currentThread().getName()+ " take: " + poll.getUrl());
                shareData.getHref(poll);
                waittime = 500;
            }

            try {
                Thread.sleep(waittime);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
